/*
 * @brief udp 连接管理
 * @auther aolei1024@gmail.com
 * @date 2019-08-26
 */
#ifndef _UDPCONNECT_H_
#define _UDPCONNECT_H_

#include <string>
#include <netinet/in.h>
#include <sstream>
#include <mutex>
#include <atomic>


class UdpConnect
{
    public:
        //TODO:等下这两个静态成员函数需要挪到公用文件中
        static void intToByte(int i,char *bytes,int size = 4);
        static int bytesToInt(char* bytes,int size = 4);

    public:
        UdpConnect(std::string host,int p);
        UdpConnect(struct sockaddr_in addrs,int fd);
        ~UdpConnect();
        void send( const void *data, int size);
        int recv( void *data, int maxsize,struct sockaddr_in& clent_addr);
        int bind();
        void init();
        int put(char* data,int len);
        int recv(char* data,int len);
        int GetFd(){return fd;}
    private:
        std::string ip;
        int port = 0;
        struct sockaddr_in addr; 
        int fd = -1;
        std::stringbuf recvbuf; 
        std::atomic_flag lock = ATOMIC_FLAG_INIT;
};

#endif //_UDPCONNECT_H_
